package com.xydtech.ecard.ecard.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.annotation.RsaRequest;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.http.HttpClientUtil;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/*
 * 分岗管户统计数据接口controller
 * @author lin
 * @version 1.0
 * @create 2024/9/11 8:40
 */
@Controller
@Api(tags = "分岗管户-统计数据")
@RequestMapping("/ecardSaasApi/fggh")
public class FgghApiController extends EcardApiBaseController {
    @Autowired
    private IEcardApiUserService ecardApiUserService;
    /**
     * 营销e助手接口服务地址
     */
    @Value("${url.apiMarketingUrl}")
    private String apiFgghUrl;

    @ApiOperation(value = "分岗管户-查询首页数据", notes = "分岗管户-查询首页数据")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = ResponseModel.class)})
    @PostMapping("/getMainDpsData")
    @ResponseBody
    @RsaRequest(param = false)
    public ResponseModel getMainDpsData() {
        try {
            String salt = getSalt();//盐值
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            Map<String, Object> paramMap = new HashMap<>();
            String apiUrl = apiFgghUrl + "/fgghApi/getMainDpsData";
            paramMap.put("userCode", userInfo.getUserCode());
            String result = HttpClientUtil.doPost(apiUrl, null, paramMap);
            JSONObject json = JSON.parseObject(result);
            if ("0".equals(json.getString("code"))) {
                return ResponseModel.ok(json.getJSONObject("data"));
            }else{
                return ResponseModel.error(json.getString("msg"));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("查询分岗管户数据失败");
        }
    }

    @ApiOperation(value = "分岗管户-查询存款分级数据", notes = "分岗管户-查询存款分级数据")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = ResponseModel.class)})
    @PostMapping("/getDpsLevelData")
    @ResponseBody
    @RsaRequest(param = false)
    public ResponseModel getDpsLevelData() {
        try {
            String salt = getSalt();//盐值
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            Map<String, Object> paramMap = new HashMap<>();
            String apiUrl = apiFgghUrl + "/fgghApi/getDpsLevelData";
            paramMap.put("userCode", userInfo.getUserCode());
            String result = HttpClientUtil.doPost(apiUrl, null, paramMap);
            JSONObject json = JSON.parseObject(result);
            if ("0".equals(json.getString("code"))) {
                return ResponseModel.ok(json.getJSONArray("data"));
            }else{
                return ResponseModel.error(json.getString("msg"));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("查询存款分级数据失败");
        }
    }

}
